package cn.krund.lyc.hotlerobot.wifimanage;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import cn.krund.lyc.hotlerobot.R;

/**
 * Created by Office on 2015/10/20.
 */
public class MyWifiAdapter extends BaseAdapter {
    private List<ScanResult> results;
    private Context mContext;
    private LayoutInflater inflater;
    private WifiManager mWifiManager;
    private WifiInfo connInfo;
    ConnectivityManager cm;
    HashMap<Integer,View> lmap = new HashMap<Integer,View>();
    public void setDatas(List<ScanResult> datas) {
        this.results = datas;
        connInfo = mWifiManager.getConnectionInfo();
    }
    public MyWifiAdapter(Context context, List<ScanResult> result) {

        this.mContext = context;
        this.results = result;
        inflater = LayoutInflater.from(mContext);
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        connInfo = mWifiManager.getConnectionInfo();
        cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    }
    @Override
    public int getCount() {
        if (results == null) {
            return  0;
        }
        return results.size();
    }

    @Override
    public Object getItem(int position) {
        return results.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder  = null;
        if (lmap.get(position)==null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.wifi_list_item,null);
            holder.tv_wifiName = (TextView) convertView.findViewById(R.id.tv_wifiname);
            holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
            lmap.put(position, convertView);
            convertView.setTag(holder);
        }else{
            convertView = lmap.get(position);
            holder = (ViewHolder) convertView.getTag();
        }
        final int currentPosition=position;
        ScanResult scanResult = results.get(currentPosition);
        holder.tv_wifiName.setText(scanResult.SSID);
        String str = scanResult.capabilities;
        if (str.contains("WPA-PSK")) {
            if (str.contains("WPA2-PSK")) {
                holder.tv_type.setText("通过WPA/WPA2受保护");
            } else {
                holder.tv_type.setText("通过WPA受保护");
            }
        }
        if (str.contains("WPA2-PSK")) {
            if (str.contains("WPA-PSK")) {
                holder.tv_type.setText("通过WPA/WPA2受保护");
            }else{
                holder.tv_type.setText("通过WPA2受保护");
            }
        }
        if (str.contains("WEP")){
            holder.tv_type.setText("通过WEP受保护");
        }
        if (str.equals("[ESS]")) {
            holder.tv_type.setText("通过加密类型受保护");
        }
        //是否连接，如果刚刚断开连接SSID = null;
        connInfo = mWifiManager.getConnectionInfo();
        NetworkInfo.State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if (wifi == NetworkInfo.State.CONNECTED) {
            WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
            String ssid = wifiInfo.getSSID();
            String conn = "\""+results.get(currentPosition).SSID+"\"";
            if (conn.endsWith(ssid)) {
                holder.tv_type.setText("已连接");
            }

        }
        // 网络信号强度
        int level = results.get(currentPosition).level;
        int imgId = R.drawable.wifi05;
        if (Math.abs(level) >= 100) {
            imgId = R.drawable.wifi05;
        } else if (Math.abs(level) >= 80) {
            imgId = R.drawable.wifi04;
        } else if (Math.abs(level) >= 70) {
            imgId = R.drawable.wifi04;
        } else if (Math.abs(level) >= 60) {
            imgId = R.drawable.wifi03;
        } else if (Math.abs(level) >= 50) {
            imgId = R.drawable.wifi02;
        } else {
            imgId = R.drawable.wifi01;
        }
        holder.iv_icon.setImageResource(imgId);
        return convertView;
    }
    class ViewHolder{
        private TextView tv_wifiName;
        private ImageView iv_icon;
        private TextView tv_type;
    }
}
